<template>
  <div class="md-example-child md-example-child-picker md-example-child-picker-1">
    <md-picker ref="picker"
               :cols="3"
               :data="pickerData"
               is-view
               :default-index="pickerDefaultIndex"
               is-cascade
               @initialed="onPickerInitialed"
               @change="onPickerConfirm"></md-picker>
  </div>
</template>
<script>
import {Picker} from 'mand-mobile'
// import district from 'mand-mobile/components/picker/demo/data/district'

export default {
  name: 'picker-demo',
  props: {
    provincename: '',
    cityname: '',
    areaname: ''
  },
  components: {
    [Picker.name]: Picker
  },
  data () {
    return {
      pickerData: [],
      pickerDefaultIndex: [],
      pickerValue: ''
    }
  },
  mounted () {
    this.pickerData = window.$mandAddress
    this.setDefaultAddress()
  },
  methods: {
    checkValue (value) {
      if (value && value.length > 0) {
        return true
      }
    },
    // 设置默认的数据
    setDefaultAddress () {
      if (this.checkValue(this.provincename) && this.checkValue(this.cityname) && this.checkValue(this.areaname)) {
        // 省市区
        let sheng = this.provincename
        let shi = this.cityname
        let qu = this.areaname
        let area = this.pickerData[0]
        let shengIndex = 0
        let shiIndex = 0
        let quIndex = 0
        for (let i = 0; i < area.length; i++) {
          if (area[i].label === sheng) {
            let shis = area[i].children
            for (let j = 0; j < shis.length; j++) {
              if (shis[j].label === shi) {
                let qus = shis[j].children
                for (let k = 0; k < qus.length; k++) {
                  if (qus[k].label === qu) {
                    shengIndex = i
                    shiIndex = j
                    quIndex = k
                  }
                }
              }
            }
            break
          }
        }
        this.pickerDefaultIndex = [shengIndex, shiIndex, quIndex]
      } else {
        this.pickerDefaultIndex = [0, 0, 0]
      }
    },
    // 初始化数据，返回去
    onPickerInitialed () {
      this.submitFatherInfo()
    },
    // 公共方法，停住滑动后告诉父类当前的选中值
    submitFatherInfo () {
      // 获取组件
      let picker = this.$refs.picker
      // picker.getColumnValues() : 获取现在所有的值(省市区)
      let sheng = picker.getColumnValue(0)
      let shi = picker.getColumnValue(1)
      let qu = picker.getColumnValue(2)
      // 通知父类
      this.$emit('pickerPause', sheng, shi, qu)
    },
    // 获取 值，停止滑动后出发的事件
    onPickerConfirm (columnIndex, itemIndex, value) {
      // let picker = this.$refs.picker
      // let sheng = picker.getColumnValue(0)
      if (value) {
        this.submitFatherInfo()
      }
    },
    getColumnValue (picker, index) {
      const value = this.$refs.picker.getColumnValue(index)
      delete value.children
    },
    getColumnIndex (picker, index) {
      // const value = this.$refs.picker.getColumnIndex(index)
      // Dialog.alert({
      //   content: `<pre>${JSON.stringify(value)}</pre>`
      // })
    }
  }
}
</script>
